﻿#ifndef ASCOMMONTEMPLATE_HPP_
#define ASCOMMONTEMPLATE_HPP_

#include <QString>
//TypeHandler管理
template<typename _ASTYPE,typename _REALTYPE>
class AsTypeHandlerManger
{
public:
	AsTypeHandlerManger()
		:m_isInited(false){}

	~AsTypeHandlerManger()
	{
		qDeleteAll(m_mapHandler);
	}

	void registerHandler(QString typeName, _ASTYPE* pHandler) 
	{
		if (m_mapHandler.contains(typeName)) {
			delete m_mapHandler[typeName];
			m_mapHandler[typeName] = nullptr;
		}
		m_mapHandler[typeName] = pHandler;
	}

	_ASTYPE* getHandler(QString typeName)
	{
		if (m_mapHandler.contains(typeName)) {
			return m_mapHandler[typeName];
		}
		return nullptr;
	}

public:
	static _REALTYPE* getInstance()
	{
		static _REALTYPE manger;
		manger.init();
		return &manger;
	}

public:
	virtual void onInit() {};
	void init() 
	{
		if (!m_isInited)
		{
			onInit();
			m_isInited = true;
		}
	}

private:
	bool m_isInited;
	QMap<QString, _ASTYPE*> m_mapHandler;

};



#endif //!ASCOMMONTEMPLATE_HPP_

